package jzqb.service.impl;

import haha.life.common.BelongToSqlUtils;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.CGDao;
import jzqb.po.AdminUserPo;
import jzqb.po.CGPo;
import jzqb.service.AdminCGService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 成果库业务层实现类
 */
@Service
public class AdminCGServiceImpl implements AdminCGService {

    @Autowired
    private CGDao cgDao;

    @Autowired
    private AdminUserInfoDao adminUserInfoDao;

    @Override
    public Boolean insertCG(CGPo po){
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        return cgDao.insertCG(po);
    }

    @Override
    public Boolean updateCG(CGPo po){
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        return cgDao.updateCG(po);
    }

    @Override
    public Boolean deleteCG(CGPo cgPo) {
        return cgDao.deleteCG(cgPo);
    }

    @Override
    public List<CGPo> queryCG(AdminQueryBo bo) {
        String[] standardType = bo.getStandardType();
        String type ="";
        List<AdminUserPo> allUserList = null;//全部用户
        List<CGPo>  list = null;
        if(standardType.length == 8){
            type = null;
        }else{
            type = BelongToSqlUtils.getBelongToSql(standardType, "领域");
        }
        bo.setType(type);
        list = cgDao.queryCG(bo);
        if (list != null && !list.isEmpty()) {
            allUserList = adminUserInfoDao.findAllUser();
            for (CGPo po : list) {
                if (allUserList != null && !allUserList.isEmpty()) {
                    for (AdminUserPo userPo : allUserList) {
                        if (po.getRecorder().equals(userPo.getId())) {
                            po.setRecorderName(userPo.getName());
                            break;
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public Long count(AdminQueryBo bo) {
        return cgDao.count(bo);
    }


}
